package org.zjt.demo

import scala.collection.mutable.ArrayBuffer

/**
  * DESC    
  *
  * @author
  * @create 2017-04-24 下午2:04
  **/
object TestArray {

  def main(args: Array[String]): Unit = {


    // TODO: 定长数组的初始化
    val numArray = new Array[Int](10);

    for (i <- 0 until 10)
      numArray.update(i, i + 10)
    numArray.foreach(println)

    for (i <- 0 until 10)
      numArray(i) = i
    numArray.foreach(println)

    val stringArray = Array("first", "second")
    stringArray.foreach(println);





    // TODO: 可变长度的数组
    val arrayBuffer = ArrayBuffer[String]()

    // TODO: += 追加一个或者多个元素在数组中。
    for (i <- 0 until 10)
      arrayBuffer += "zhang_" + i
    arrayBuffer.foreach(println)

    for (i <- 0 until 10)
      arrayBuffer += ("zhang_" + i, "wang_" + i)
    arrayBuffer.foreach(println)

    // TODO: ++= 追加一个数组、list
    arrayBuffer ++= stringArray

    println(arrayBuffer.toArray.length)

    // TODO: 删除末尾的10个元素
    arrayBuffer.trimEnd(10)

    // TODO: array和arraybuffer转换
    arrayBuffer.toArray //转换为定长数组
    stringArray.toBuffer


    // TODO: 索引遍历和倒序遍历
    for (i <- 0 until arrayBuffer.length)
      println(arrayBuffer(i))

    for (i <- (0 until arrayBuffer.length).reverse)
      println(i)
    (for (i <- (0 until arrayBuffer.length).reverse if i % 2 == 0) yield arrayBuffer(i).concat("_now")).foreach(println)


    // TODO: 转化为以逗号 ，分解的字符串
    println(arrayBuffer.mkString(","))
    println(arrayBuffer.mkString("[", ",", "]"))


    var multiDimArr = Array(Array(1, 4, 3, 5, 2), Array(6, 4, 2, 1, 2))

    for (array <- multiDimArr) {
      for (element <- array)
        println(element)
    }
    println(multiDimArr(0)(2))

    var array2 = Array(12343, 23, 1223, 321)
    printf("min:%s max :%s  sum:%s  data:%s",array2.min ,array2.max ,array2.sum , array2.toString )

  }

}
